﻿using KZ.Express.ENT;
using KZ.Express.Presenter;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KZ.Express.Setup.PriceList
{
    public partial class OriginalPriceListView : FormBase
    {
        #region Private Variables

        bool _isVisible;
        new PriceListPresenter _presenter;

        #endregion

        #region Private Methods

        private void OriginalPriceListView_Load(object sender, EventArgs e)
        {
            LoadCarrier();
            LoadData();
        }

        private void OriginalPriceListView_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible && !_isVisible)
                LoadData();
            _isVisible = this.Visible;
        }

        private void txtPriceListCode_TextChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void txtPriceListName_TextChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void txtSupplierCode_TextChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void cmbCarrierID_SelectedValueChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        protected override void btnViewAll_Click(object sender, EventArgs e)
        {
            try
            {
                txtPriceListCode.TextChanged -= txtPriceListCode_TextChanged;
                txtPriceListName.TextChanged -= txtPriceListName_TextChanged;
                txtSupplierCode.TextChanged -= txtSupplierCode_TextChanged;
                cmbCarrierID.SelectedValueChanged -= cmbCarrierID_SelectedValueChanged;

                base.btnViewAll_Click(sender, e);
            }
            finally
            {
                txtPriceListCode.TextChanged += txtPriceListCode_TextChanged;
                txtPriceListName.TextChanged += txtPriceListName_TextChanged;
                txtSupplierCode.TextChanged += txtSupplierCode_TextChanged;
                cmbCarrierID.SelectedValueChanged += cmbCarrierID_SelectedValueChanged;
            }
        }

        private void LoadCarrier()
        {
            try
            {
                cmbCarrierID.SelectedValueChanged -= cmbCarrierID_SelectedValueChanged;
                Global.LoadComboBox(cmbCarrierID, _presenter.LoadForCMB<CarrierENT>(null));
                cmbCarrierID.SelectedValueChanged += cmbCarrierID_SelectedValueChanged;
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        #endregion

        #region Constructors

        public OriginalPriceListView()
        {
            InitializeComponent();
            base.ObjectID = (int)Objects.OriginalPriceList;

            base.Action = ActionName.List;
            base.InitializeDataGrid(dtgPriceList);
            base._presenter = _presenter = new PriceListPresenter(this);

            colEffectedDate.DefaultCellStyle.Format = Utility.Info.DateTimeFormat.ShortDatePattern;
            colExpiredDate.DefaultCellStyle.Format = Utility.Info.DateTimeFormat.ShortDatePattern;

            btnViewAll.Click += btnViewAll_Click;
            dtgPriceList.ColumnHeaderMouseClick += dtg_ColumnHeaderMouseClick;

            Global.SetDoubleBuffered(dtgPriceList);
        }

        #endregion

        #region Public Properties

        [Search]
        public int IsOriginal
        {
            get { return 1; }
        }

        [Search]
        public string PriceListCode
        {
            get { return txtPriceListCode.Text; }
            set { txtPriceListCode.Text = value; }
        }

        [Search]
        public string PriceListName
        {
            get { return txtPriceListName.Text; }
            set { txtPriceListName.Text = value; }
        }

        [Search]
        public string SupplierCode
        {
            get { return txtSupplierCode.Text; }
            set { txtSupplierCode.Text = value; }
        }

        [Search]
        public int CarrierID
        {
            get { return Utility.IntParse(cmbCarrierID.SelectedValue); }
            set { cmbCarrierID.SelectedValue = value; }
        }

        public DataTable Carriers
        {
            set { cmbCarrierID.DataSource = value; }
        }

        #endregion
    }
}
